package com.hkemmm.control;

import com.hkemmm.model.UserModel;
import com.hkemmm.utils.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");

        String userName = request.getParameter("userName");
        String userPsw = request.getParameter("userPsw");



//        if(request.getSession().isNew()){
//            System.out.println("是新用户");
//        }else{
//            System.out.println("不是新用户");
//        }

        //request.setAttribute("userName",userName);

        request.getSession().setAttribute("userName",userName);

        //UserModel user = new UserModel();
        if(MyUtils.getInstence(UserModel.class).canLogin(userName,userPsw)){
            //重定向
            //不可以访问站点安全目录，不是同一个request，无法携带数据
            //response.sendRedirect("views/Hplus-v.4.1.0/index.html");

            request.getRequestDispatcher("WEB-INF/viewpages/main.jsp").forward(request,response);
        }else {
            //转发
            //可以访问站点安全目录，是同一个request，可以携带数据
            request.getRequestDispatcher("test.jsp").forward(request,response);
        }
    }
}
